<?php

require_once("lib/exception/duplicate_instance_exception.php");
require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/instance_not_found_exception.php");
require_once("lib/exception/sql_exception.php");

require_once("util/connection.php");

require_once("model/autor_message/dao/autor_message_dao.php");
require_once("model/autor_message/to/autor_message_to.php");


class Mysql_autor_message_dao extends Autor_message_dao{

	public function create($autor_message_to) {
		if ($this->exist($autor_message_to->get_number())) {
			throw new Duplicate_instance_exception(get_class($this));
		}
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$from_autor_login = mysql_real_escape_string($autor_message_to->get_from_autor_login());
			$to_autor_login = mysql_real_escape_string($autor_message_to->get_to_autor_login());
			$title = mysql_real_escape_string($autor_message_to->get_title());
			$content = mysql_real_escape_string($autor_message_to->get_content());
			$date = mysql_real_escape_string($autor_message_to->get_date());
			$state = mysql_real_escape_string($autor_message_to->get_state());
			
			$result = mysql_query("insert into autor_message 
									(from_autor_login, to_autor_login, title, content, date, state) 
									values 
									('$from_autor_login', '$to_autor_login', '$title',
									'$content', '$date', '$state')", $link);			
			
			if (!$result || (mysql_affected_rows($link) != 1) 
					|| (($number = mysql_insert_id($link)) == 0)) 
				throw new Sql_exception("Not row inserted in " . get_class($this));
			
			Connection::singleton()->transaction_commit($link);
			
			$autor_message_to = new Autor_message_to($number, $from_autor_login, $to_autor_login, 
														$title,	$content, $date, $state);
			return $autor_message_to;
		}
		catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
	
	public function exist($number) {

		try {
			$link = Connection::singleton()->transaction_begin();		
			
			$result = mysql_query("select * from autor_message 
									where number = '$number'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1)) {
				Connection::singleton()->transaction_commit($link);
				return false;
			}
			
			Connection::singleton()->transaction_commit($link);			
			return true;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
	}
	
	
	public function find_by_number($number) {
	
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$result = mysql_query("select * from autor_message 
									where number = '$number'", $link);
									
			if (mysql_num_rows($result) == 0) 
				throw new Instance_not_found_exception(get_class($this));
			
			if (mysql_num_rows($result) != 1) 
				throw new Sql_exception(get_class($this));

			$aux = mysql_fetch_assoc($result);
			Connection::singleton()->transaction_commit($link);
			return new Autor_message_to($aux['number'], $aux['from_autor_login'], 
									$aux['to_autor_login'], $aux['title'],
									$aux['content'], $aux['date'], $aux['state']);
			
		}
		catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
	public function count_inbox_messages_by_login($login) {
		try {	
			$link = Connection::singleton()->transaction_begin();

			$login = mysql_real_escape_string($login);
			
			$result = mysql_query("select COUNT(number) as count from autor_message 
									where to_autor_login like '$login'
									and state not like 'delete'" , $link);
			if (mysql_num_rows($result) == 0) {
				throw new Instance_not_found_exception(get_class($this));
			}
			
			$aux = mysql_fetch_assoc($result);
			
			Connection::singleton()->transaction_commit($link);
									
			return $aux['count'];
						
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
	}
	
	public function count_new_messages_by_login($login) {
		try {	
			$link = Connection::singleton()->transaction_begin();

			$login = mysql_real_escape_string($login);
			
			$result = mysql_query("select COUNT(number) as count from autor_message 
									where to_autor_login like '$login'
									and state like 'new'", $link);
			
			if (mysql_num_rows($result) == 0) {
				throw new Instance_not_found_exception(get_class($this));
			}
			
			$aux = mysql_fetch_assoc($result);
			
			Connection::singleton()->transaction_commit($link);
									
			return $aux['count'];
						
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
	}
	
	public function count_send_messages_by_login($login) {
		try {	
			$link = Connection::singleton()->transaction_begin();

			$login = mysql_real_escape_string($login);
			
			$result = mysql_query("select COUNT(number) as count from autor_message 
									where from_autor_login like '$login'
									and state like 'new'", $link);
			
			if (mysql_num_rows($result) == 0) {
				throw new Instance_not_found_exception(get_class($this));
			}
			
			$aux = mysql_fetch_assoc($result);
			
			Connection::singleton()->transaction_commit($link);
									
			return $aux['count'];
						
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
	}
	
	public function find_inbox_by_login($login, $start_index, $count) {
	
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$login = mysql_real_escape_string($login);
			
			$result = mysql_query("select * from autor_message 
									where to_autor_login like '$login'
									and state not like 'delete'
									order by date desc", $link);
									
			if (mysql_num_rows($result) == 0) {
				throw new Instance_not_found_exception(get_class($this));
			}

			$messages_list = NULL;
			$i = 0;

			if ($start_index < (mysql_num_rows($result)) && (mysql_data_seek($result,$start_index))) {	
				while (($aux = mysql_fetch_assoc($result)) && ($i <= $count)) 
					$messages_list[$i++] = new Autor_message_to($aux['number'], $aux['from_autor_login'], 
																$aux['to_autor_login'], $aux['title'],
																$aux['content'], $aux['date'], $aux['state']);
			}
			Connection::singleton()->transaction_commit($link);						
			return $messages_list;
											
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}	
	}
	

	public function find_send_by_login($login, $start_index, $count) {
	
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$login = mysql_real_escape_string($login);
			
			$result = mysql_query("select * from autor_message 
									where from_autor_login like '$login'
									and state not like 'delete'
									order by date desc", $link);
									
			if (mysql_num_rows($result) == 0) {
				throw new Instance_not_found_exception(get_class($this));
			}
									

			$messages_list = NULL;
			$i = 0;

			if ($start_index < (mysql_num_rows($result)) && (mysql_data_seek($result,$start_index))) {	
				while (($aux = mysql_fetch_assoc($result)) && ($i <= $count)) 
					$messages_list[$i++] = new Autor_message_to($aux['number'], $aux['from_autor_login'], 
																$aux['to_autor_login'], $aux['title'],
																$aux['content'], $aux['date'], $aux['state']);
			}
			Connection::singleton()->transaction_commit($link);						
			return $messages_list;
											
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}	
	}
	
	public function update($autor_message_to) {
	
		try {
				
			$link = Connection::singleton()->transaction_begin();
			
			$number = $autor_message_to->get_number();
			$state = mysql_real_escape_string($autor_message_to->get_state());
			
			$result = mysql_query("update autor_message 
									set state='$state' 
									where number = '$number'", $link);
							
			if (mysql_affected_rows($link) == 0) 
				throw new Instance_not_found_exception(get_class($this));
			
			if (mysql_affected_rows($link) != 1) 
				throw new Sql_exception("Not row removed in " . get_class($this));
			
			Connection::singleton()->transaction_commit($link);
			
			return $autor_message_to;
		}
		catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);		
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}		
	}
	
	
	public function remove($number) {	
		try {			
			$link = Connection::singleton()->transaction_begin();		
			
			$result = mysql_query("update autor_message 
									set state='delete' 
									where number = '$number'", $link);
									
			if (mysql_affected_rows($link) == 0) 
				throw new Instance_not_found_exception(get_class($this));
			
			if (mysql_affected_rows($link) != 1) 
				throw new Sql_exception("Not row removed in " . get_class($this));
			
			Connection::singleton()->transaction_commit($link);
					
		}
		catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);		
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
	}
	
	
}
?>